# 本文件用于分析文本长度分布
# 功能：计算文本长度统计、长度分布和特征分析
# 返回内容说明（JSON格式）：
#   - avg_length: 平均长度 (float) - 包含所有字符
#   - min_length: 最短文本长度 (int)
#   - max_length: 最长文本长度 (int)
#   - length_frequency: 0~30每个长度的频数 (list) - 用于折线面积图
#       * 注意：length_frequency[30] 表示长度=30的频数（不是30+）
#   - length_distribution: 长度分布统计（按指定区间）
#       - bins: 区间名称列表 (list)
#       - counts: 各区间样本数量列表 (list)
#       * 30+区间 = length_frequency[30] (长度=30) + 长度>30的频数
#   - feature_analysis: 文本特征分析
#       - has_digit: 包含数字的文本数量 (int)
#       - is_only_text: 纯文字文本数量 (int)
import json
import re
from collections import Counter


def analyze_text_lengths(data):
    """分析文本长度分布和特征"""
    texts = [item['text'] for item in data.values()]

    # 长度统计
    lengths = [len(text) for text in texts]
    avg_length = round(sum(lengths) / len(lengths), 2)
    min_length = min(lengths)
    max_length = max(lengths)

    # 0~30每个长度的频数（用于折线面积图）
    length_frequency = [0] * 32  # 索引0~30 (长度0~30)
    for l in lengths:
        if l <= 30:
            length_frequency[l] += 1
        else:
            length_frequency[31] += 1

    # 长度分布（按0-5,6-11等区间）
    bins = [
        "0-5", "6-11", "12-17", "18-23", "24-29", "30+"
    ]
    counts = [0] * 6
    for l in lengths:
        if l <= 5:
            counts[0] += 1
        elif l <= 11:
            counts[1] += 1
        elif l <= 17:
            counts[2] += 1
        elif l <= 23:
            counts[3] += 1
        elif l <= 29:
            counts[4] += 1
        else:  # l >= 30
            counts[5] += 1

    # 特征分析
    has_digit = sum(1 for text in texts if re.search(r'\d', text))
    is_only_text = sum(1 for text in texts if re.match(r'^[\u4e00-\u9fa5a-zA-Z]+$', text))

    return {
        "avg_length": avg_length,
        "min_length": min_length,
        "max_length": max_length,
        "length_frequency": length_frequency,  # 0~30每个长度的频数（长度>=30的频数在index[31]）
        "length_distribution": {
            "bins": bins,
            "counts": counts
        },
        "feature_analysis": {
            "has_digit": has_digit,
            "is_only_text": is_only_text
        }
    }


if __name__ == "__main__":
    # 测试用例
    from data_load import load_data

    data = load_data()
    analysis = analyze_text_lengths(data)
    print(json.dumps(analysis, indent=2, ensure_ascii=False))